package com.uzhie.web.controller.http;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.uzhie.exception.CatalogException;
import com.uzhie.exception.MonitorException;
import com.uzhie.web.anno.Auth;
import com.uzhie.web.bean.PageBean;
import com.uzhie.web.bean.PayloadBean;
import com.uzhie.web.controller.http.comm.CommController;
import com.uzhie.web.module.Catalog;
import com.uzhie.web.service.CatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by Zhangdequan on 2018/7/18.
 */
@RestController
@RequestMapping(value = "catalog")
public class CatalogController extends CommController {

    @Autowired
    private CatalogService catalogService;

    /**
     * 查询返回所有的目录数据(树状结构)
     *
     * @param body
     * @return
     * @throws IllegalAccessException
     * @throws InstantiationException
     */
    @Auth
    @RequestMapping(value = "treeList")
    public PayloadBean selectAllCatalogToTree(@RequestBody(required = false) JSONObject body) {
        try {
            List<Catalog> list = catalogService.selectAllCatalogToTree();
            return buildResponse(list);
        } catch (IllegalArgumentException e) {
            return buildResponse(null, setError(0, e.getMessage()));
        }
    }

    /**
     * 根据部门id,查询子节点信息(树状结构)
     *
     * @param body
     * @return
     * @throws IllegalAccessException
     * @throws InstantiationException
     */
    @Auth
    @RequestMapping(value = "treeListbyCatalogId")
    public PayloadBean selectCatalogByDepartToTree(@RequestBody(required = false) JSONObject body) {

        List<Catalog> list = null;
        try {
            list = catalogService.selectCatalogByDepartToTree(body.getString("catalogId"));
            return buildResponse(list);
        } catch (IllegalArgumentException e) {
            return buildResponse(null, setError(0, e.getMessage()));
        }
    }

    /**
     * 通过父节点，查询子节点的目录列表
     *
     * @param body
     * @return
     * @throws IllegalAccessException
     * @throws InstantiationException
     */
    @Auth
    @RequestMapping(value = "list")
    public PayloadBean selectAllCatalogList(@RequestBody(required = false) JSONObject body, @RequestBody(required = false) PageBean page) {
        List<Catalog> list = null;
        try {
            list = catalogService.selectAllCatalogList(body.getString("parentId"), page);
            return buildResponseWithPage(list, page);
        } catch (IllegalArgumentException e) {
            return buildResponseWithPage(null, page, e.getMessage());
        }
    }

    /**
     * 添加目录对象
     *
     * @param body
     * @return
     * @throws IllegalAccessException
     * @throws InstantiationException
     */
    @Auth
    @RequestMapping(value = "addCatalog")
    public PayloadBean addCatalog(@RequestBody(required = false) JSONObject body) {
        try {
            catalogService.addCatalog(JSON.toJavaObject(body, Catalog.class));
            return buildResponse(null);
        } catch (CatalogException e) {
            return buildResponse(null, e.getMessage());
        }
    }

    /**
     * 根据目录id,查询目录详情
     *
     * @param body
     * @return
     * @throws IllegalAccessException
     * @throws InstantiationException
     */
    @Auth
    @RequestMapping(value = "selectCatalogById")
    public PayloadBean selectCatalogById(@RequestBody(required = false) JSONObject body) {
        Catalog catalog = null;
        try {
            catalog = catalogService.selectCatalogById(body.getString("catalogId"));
            return buildResponse(catalog);
        } catch (IllegalArgumentException e) {
            return buildResponse(null, e.getMessage());
        }
    }

    /**
     * 更新目录对象
     *
     * @param body
     * @return
     * @throws IllegalAccessException
     * @throws InstantiationException
     */
    @Auth
    @RequestMapping(value = "updateCatalog")
    public PayloadBean updateCatalog(@RequestBody(required = false) JSONObject body) {
        try {
            catalogService.updateCatalog(JSON.toJavaObject(body, Catalog.class));
            return buildResponse(null);
        } catch (CatalogException e) {
            return buildResponse(null, e.getMessage());
        }
    }


    /**
     * 根据目录id,更新目录状态信息
     *
     * @param body
     * @return
     * @throws IllegalAccessException
     * @throws InstantiationException
     */
    @Auth
    @RequestMapping(value = "updateStatus")
    public PayloadBean updateRoleStatusById(@RequestBody(required = false) JSONObject body) {
        try {
            int status = body.getInteger("status");
            List<Catalog> catalogIds = JSONArray.parseArray(body.getString("catalogIds"), Catalog.class);
            catalogService.updateCatalogStatusById(catalogIds, status);
            return buildResponse(null);
        } catch (CatalogException e) {
            return buildResponse(null, e.getMessage());
        }
    }


    /**
     * 返回所有车辆数据,包含车辆目录节点,在用户绑定车辆关系时使用, 只查询了车辆名称和id属性
     * created by lilongfei
     *
     * @return
     **/
    @Auth
    @RequestMapping(value = "treeAndCar")
    public PayloadBean selectAllCatalogAndCar(@RequestBody(required = false) JSONObject body) {
        List<Catalog> list = null;
        try {
            String userId = body.getString("userId");
            list = catalogService.selectAllCatalogAndCar(userId);
            return buildResponse(list);
        } catch (IllegalArgumentException | MonitorException e) {
            return buildResponse(null, e.getMessage());
        }
    }

    @Auth
    @RequestMapping(value = "selectCatalogByUserPower")
    public PayloadBean selectCatalogByUserPower(@RequestBody(required = false) JSONObject body) {
        try {
            List<Catalog> list = catalogService.selectCatalogByUserPower(body.getString("userId"));
            return buildResponse(list);
        } catch (IllegalArgumentException | MonitorException e) {
            return buildResponse(null, e.getMessage());
        }
    }

}
